package csel.view.status;

import java.awt.Color;
import csel.model.*;
import csel.model.entity.Character;
import csel.model.items.EquippableItem;
import csel.view.RepresentationFactory;

import javax.swing.*;

public class EquipmentBar extends JPanel implements Observer<Object>
{
	private static final long serialVersionUID = 1L;

	private Character myCharacter;
	
	private JLabel[] equipLabels;
	private JLabel unequipIcon;
	private JLabel rotateIcon;
	private JLabel invIcon;
	private JLabel skillIcon;
	
	private ImageIcon emptyIcon;
	
	public static EquipmentBar initialize(Character myCharacter)
	{
		EquipmentBar myEquipBar = new EquipmentBar(myCharacter);
		myCharacter.addObserver(myEquipBar);
		return myEquipBar;
	}
	
	private EquipmentBar(Character myCharacter)
	{
		this.myCharacter = myCharacter;
		
		setLayout(new BoxLayout(this,BoxLayout.LINE_AXIS));
		
		equipLabels = new JLabel[10];
		for (int a = 0; a < 10; ++a)
		{
			JLabel tempLabel = new JLabel(Integer.toString((a+1) % 10));
			tempLabel.setVerticalAlignment(JLabel.NORTH);
			tempLabel.setForeground(Color.white);
			add(tempLabel);
			add(equipLabels[a] = new JLabel());
		}
		
		unequipIcon = ImageLoader.makeLabel("unequip_equip_icon");
		rotateIcon = ImageLoader.makeLabel("rotate_icon");
		invIcon = ImageLoader.makeLabel("inv_mode_icon");
		skillIcon = ImageLoader.makeLabel("skill_mode_icon");
		emptyIcon = ImageLoader.makeIcon("emptyicon");
		
		JLabel tempLabel1 = new JLabel(" - ");
		tempLabel1.setVerticalAlignment(JLabel.NORTH);
		tempLabel1.setForeground(Color.white);
		add(tempLabel1);
		add(unequipIcon);
		
		tempLabel1 = new JLabel(" + ");
		tempLabel1.setVerticalAlignment(JLabel.NORTH);
		tempLabel1.setForeground(Color.white);
		add(tempLabel1);
		add(rotateIcon);
		
		tempLabel1 = new JLabel(" I ");
		tempLabel1.setVerticalAlignment(JLabel.NORTH);
		tempLabel1.setForeground(Color.white);
		add(tempLabel1);
		add(invIcon);
		
		tempLabel1 = new JLabel(" T ");
		tempLabel1.setVerticalAlignment(JLabel.NORTH);
		tempLabel1.setForeground(Color.white);
		add(tempLabel1);
		add(skillIcon);
		
		setLabels();
		
		setOpaque(false);
	}
	
	
	private void setLabels()
	{
		//Head, Torso, Legs, Shoes, Lefthand, Righthand
		EquippedItems myEquipment = myCharacter.getEquipment();
		EquippableItem myItem = null;
		
		for (int a = 6; a < 10; ++a)
		{
			equipLabels[a].setIcon(emptyIcon);
		}
		
		//get ready for some super shitty code!
		
		myItem = myEquipment.getEquippedItem(BodyParts.Head);
		if (myItem == null)
		{
			equipLabels[0].setIcon(emptyIcon);
		}
		else
		{
			equipLabels[0].setIcon(new ImageIcon(RepresentationFactory.getInstance().get(myItem).getIconImage()));
		}
		
		myItem = myEquipment.getEquippedItem(BodyParts.Torso);
		if (myItem == null)
		{
			equipLabels[1].setIcon(emptyIcon);
		}
		else
		{
			equipLabels[1].setIcon(new ImageIcon(RepresentationFactory.getInstance().get(myItem).getIconImage()));
		}
		
		myItem = myEquipment.getEquippedItem(BodyParts.Legs);
		if (myItem == null)
		{
			equipLabels[2].setIcon(emptyIcon);
		}
		else
		{
			equipLabels[2].setIcon(new ImageIcon(RepresentationFactory.getInstance().get(myItem).getIconImage()));
		}
		
		myItem = myEquipment.getEquippedItem(BodyParts.Shoes);
		if (myItem == null)
		{
			equipLabels[3].setIcon(emptyIcon);
		}
		else
		{
			equipLabels[3].setIcon(new ImageIcon(RepresentationFactory.getInstance().get(myItem).getIconImage()));
		}
		
		myItem = myEquipment.getEquippedItem(BodyParts.Lefthand);
		if (myItem == null)
		{
			equipLabels[4].setIcon(emptyIcon);
		}
		else
		{
			equipLabels[4].setIcon(new ImageIcon(RepresentationFactory.getInstance().get(myItem).getIconImage()));
		}
		
		myItem = myEquipment.getEquippedItem(BodyParts.Righthand);
		if (myItem == null)
		{
			equipLabels[5].setIcon(emptyIcon);
		}
		else
		{
			equipLabels[5].setIcon(new ImageIcon(RepresentationFactory.getInstance().get(myItem).getIconImage()));
		}
		
		
		revalidate();
	}
	
	public void mupdate(Object o, Object param)
	{
		if (o.equals(myCharacter))
		{
			setLabels();
		}
	}
	
	public void setCharacter(Character c)
	{
		myCharacter = c;
		myCharacter.addObserver(this);
		setLabels();
	}
	
	private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
    {
            throw new java.io.NotSerializableException("EquipmentBar should not be serialized.");
    }
    
    private void readObject(java.io.ObjectInputStream in) throws java.io.IOException
    {
            throw new java.io.NotSerializableException("EquipmentBar should not be serialized.");
    }

}
